package net.ariel.myideablog.web.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.ariel.myideablog.dao.IBlog;
import net.ariel.myideablog.dao.ICategory;
import net.ariel.myideablog.model.Blog;
import net.ariel.myideablog.model.Category;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class BlogController extends MultiActionController {

	private String viewPage;
	private IBlog iBlog;
	private ICategory iCategory;
	private String errorString;
	private int numberPerPage;
	private int pages;
	private Blog blog;
	
	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}
	
	public void setIBlog(IBlog iBlog) {
		this.iBlog = iBlog;
	}
	
	public void setErrorString(String errorString) {
		this.errorString = errorString;
	}
	
	public void setICategory(ICategory iCategory) {
		this.iCategory = iCategory;
	}
	
	public void setNumberPerPage(int numberPerPage) {
		this.numberPerPage = numberPerPage;
	}
	
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response) {
		int blogNumber = iBlog.getBlogNumber();
		if(numberPerPage != 0) {
			pages = blogNumber/numberPerPage;
			if(blogNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		List<Blog> blogs = iBlog.getBlogsByPage(0, numberPerPage);
		List<Category> categories = iCategory.getAllCategories();

		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("blogs", blogs);
		mav.addObject("categories", categories);
		mav.addObject("pages", pages);
		mav.addObject("blog", blog);
		mav.addObject("error", errorString);
		return mav;
	}
	
	public ModelAndView page(HttpServletRequest request, HttpServletResponse response) {
		String page = request.getParameter("page");
		int pageNo = Integer.parseInt(page);
		int blogNumber = iBlog.getBlogNumber();
		if(numberPerPage != 0) {
			pages = blogNumber/numberPerPage;
			if(blogNumber % numberPerPage != 0) {
				pages++;
			}
		}
		else {
			pages = 0;
		}
		List<Blog> blogs = iBlog.getBlogsByPage(pageNo*numberPerPage, (pageNo+1)*numberPerPage);
		List<Category> categories = iCategory.getAllCategories();

		ModelAndView mav = new ModelAndView();
		mav.setViewName(viewPage);
		mav.addObject("blogs", blogs);
		mav.addObject("categories", categories);
		mav.addObject("pages", pages);
		return mav;
	}
	
	public String add(HttpServletRequest request, HttpServletResponse response) {
		String title = request.getParameter("title");
		String body = request.getParameter("body");
		String categoryKey = request.getParameter("categoryKey");
		if(title == null || title.trim().isEmpty() || body == null || body.trim().isEmpty() || categoryKey == null || categoryKey.isEmpty()) {
			setErrorString("This is something wrong");
		}
		else {
			Blog blog = new Blog(title, body);
			blog.setCategoryKey(categoryKey);
			iBlog.saveBlog(blog);
			setErrorString(null);
		}
		return "redirect:/admin/blog.do";
	}
	
	public String edit(HttpServletRequest request, HttpServletResponse response) {
		String blogKey = request.getParameter("key");
		if(blogKey != null) {
			blog = iBlog.getBlog(blogKey);
		}
		return "redirect:/admin/blog.do";
	}
	
	public String update(HttpServletRequest request, HttpServletResponse response) {
		String blogKey = request.getParameter("key");
		String title = request.getParameter("title");
		String body = request.getParameter("body");
		String categoryKey = request.getParameter("categoryKey");
		if(title == null || title.trim().isEmpty() || body == null || body.trim().isEmpty() ||
				categoryKey == null || categoryKey.trim().isEmpty()) {
			setErrorString("This is something wrong");
		}
		else {
			Blog newBlog = new Blog();
			newBlog.setTitle(title);
			newBlog.setBody(body);
			newBlog.setCategoryKey(categoryKey);
			iBlog.updateBlog(blogKey, newBlog);
			blog = null;
		}
		return "redirect:/admin/blog.do";
	}
	public String delete(HttpServletRequest request, HttpServletResponse response) {
		String blogKey = request.getParameter("key");
		iBlog.deleteBlog(blogKey);
		return "redirect:/admin/blog.do";
	}
	
}
